<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加轮播图</title>
    <link rel="stylesheet" href="/static/public/layui/css/layui.css">
    <script type="text/javascript" src="/static/public/layui/layui.js"></script>
</head>
<body style="padding: 15px">
<div class="layui-form">
    @csrf
    <input type="hidden" name="bid" value="{{ $banner['bid'] }}">
    <div class="layui-form-item">
        <label class="layui-form-label">轮播图标题</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="title" value="{{ $banner['title'] }}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接地址</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="url" value="{{ $banner['url'] }}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">轮播图</label>
        <div class="layui-input-inline">
            <img id="preview_img" src="{{ $banner['thumb'] }}" style="height: 30px">

            <button type="button" class="layui-btn" id="btn_upload">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <textarea name="desc" placeholder="请输入内容" class="layui-textarea">{{ $banner['desc'] }}</textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" lay-skin="primary" {{ $banner['status']==1?'checked':'' }} name="status" value="" title="禁用">
        </div>
    </div>
</div>

<script type="text/javascript">
    layui.use(['upload','form'], function(){
        $ = layui.jquery;
        var _token =  $('input[name="_token"]').val();
        upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#btn_upload' //绑定元素
            ,url: '/admin/files/upload_img' //上传接口
            ,data:{_token:_token}
            ,done: function(res){
                //上传完毕回调
                console.log(res);
                $('#preview_img').attr('src',res.data.src);
            }
            ,error: function(){
                //请求异常回调
            }
        });

    });
    //文章保存
    function save() {
        var data = {};
        data._token   = $('input[name="_token"]').val();
        data.bid      = $('input[name="bid"]').val();
        data.title    = $.trim($('input[name="title"]').val());
        data.url = $.trim($('input[name="url"]').val());
        data.thumb    = $('#preview_img').attr('src');
        data.desc    = $('textarea[name="desc"]').val();
        data.status   = $('input[name="status"]').is(':checked')?1:0;


        if(data.title==''){
            return layer.alert('请填写轮播图标题',{icon:2});
        }
        $.post('/admin/banner/save',data,function (res) {
            if (res.code>0){
                return layer.alert(res.msg,{icon:2});
            }
            layer.msg(res.msg);
            setTimeout(function () {
                parent.window.location.reload();
            },1000);
        },'json');

    }
</script>
</body>
</html>
